﻿using System;
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.ObjectModel;

namespace HotCopperUserScanService.Tests
{
    [TestClass]
    public class ParserTests
    {
        private HotCopperBrowser _hcBrowser;
        private Collection<string> _errors;

        [TestInitialize]
        public void TestInitialize()
        {
            const string UserIDTag = "{USERID}";
            const string page = @"http://hotcopper.com.au/search/1099/?q=%2A&t=post&o=date&c[user][0]=" + UserIDTag;

            _errors = new Collection<string>();
            _hcBrowser = new HotCopperBrowser();
            //_hcBrowser.Login("caterer", "automaton", @"http://hotcopper.com.au/user_profile.asp?uid=83692");            
            //new hotcopper
            _hcBrowser.Login("caterer", "automaton", page);            
        }

        [TestCleanup]
        public void TestCleanup()
        {
            _hcBrowser.Logoff();    
        }

        [TestMethod]
        [Ignore]
        public void ParseForUser_OldHotCopper()
        {
            var html = File.ReadAllText(@"C:\Users\nickz\SkyDrive\Dev\HotCopperUserScanService\HotCopperUserScanService.Tests\view-source hotcopper.com.au user_profile.asp uid=83692.htm");
            var userName = "TimBisovic";
            var expectedDate = new DateTime(2014, 1, 20);
            var posts = Parser.ExtractPosts(html, userName, _hcBrowser, 10, _errors).ToArray();
            Console.WriteLine("Posts = {0}", posts.Length);
            foreach (var post in posts)
            {
                Assert.AreEqual(expectedDate, post.DateTime.Date);
                Assert.IsFalse(string.IsNullOrEmpty(post.Content));
                Assert.AreEqual(userName, post.Username);
            }
            Assert.IsFalse(_errors.Any());
        }

        [TestMethod]
        public void ParseForUser_NewHotCopper()
        {
            var html = File.ReadAllText(@"C:\Users\nickz\SkyDrive\Dev\HotCopperUserScanService\HotCopperUserScanService.Tests\Updated HotCopper Posts Format 9 June 2014.txt");
            var userName = "TimBisovic";
            var expectedDate = new DateTime(2014, 6, 6);
            var noOfPostsToParse = 15;
            var posts = Parser.ExtractPosts(html, userName, _hcBrowser, noOfPostsToParse, _errors).ToArray();
            Console.WriteLine("Posts = {0}", posts.Length);
            
            Assert.AreEqual(noOfPostsToParse, posts.Length, "Incorrect posts found");
            foreach (var post in posts)
            {
                Assert.AreEqual(expectedDate, post.DateTime.Date);
                Assert.IsFalse(string.IsNullOrEmpty(post.Content));
                Assert.AreEqual(userName, post.Username);
                Assert.IsFalse(string.IsNullOrEmpty(post.Uri));
            }
            Assert.IsFalse(_errors.Any());
        }

        [TestMethod]
        public void ParseForUser_GizardWithStockAndDayTraderComments()
        {
            var html = File.ReadAllText(@"C:\Users\nickz\SkyDrive\Dev\HotCopperUserScanService\HotCopperUserScanService.Tests\Gizard Example 9 June 2014.txt");
            var userName = "Gizard";
            var noOfPostsToParse = 15;
            var posts = Parser.ExtractPosts(html, userName, _hcBrowser, noOfPostsToParse, _errors).ToArray();
            Console.WriteLine("Posts = {0}", posts.Length);

            Assert.AreEqual(noOfPostsToParse, posts.Length, "Incorrect posts found");
            Assert.IsTrue(posts.Any(p => p.Code.ToUpper() == "MRF"), "Expecting the code MRF to have been parsed");
            Assert.IsTrue(posts.Any(p => p.FirstPrice.StartsWith("8.2")), "Expecting the code MRF to have been parsed");

            foreach (var post in posts)
            {
                Assert.IsTrue(post.DateTime.Date == new DateTime(2014, 6, 6) || post.DateTime.Date == new DateTime(2014, 6, 5));
                Assert.IsFalse(string.IsNullOrEmpty(post.Content));
                Assert.AreEqual(userName, post.Username);
                Assert.IsFalse(string.IsNullOrEmpty(post.Uri));
            }
            Assert.IsFalse(_errors.Any());
        }

        [TestMethod]
        public void ParseForUser_NoParagrapgTag()
        {
            var html = File.ReadAllText(@"C:\Users\nickz\SkyDrive\Dev\HotCopperUserScanService\HotCopperUserScanService.Tests\Example with a Paragraph Tag.txt");
            var userName = "TimBisovic";
            var noOfPostsToParse = 15;
            var posts = Parser.ExtractPosts(html, userName, _hcBrowser, noOfPostsToParse, _errors).ToArray();
            Console.WriteLine("Posts = {0}", posts.Length);

            Assert.AreEqual(noOfPostsToParse, posts.Length, "Incorrect posts found");
            foreach (var post in posts)
            {
                Assert.IsTrue(post.DateTime.Date == new DateTime(2014, 6, 6) || post.DateTime.Date == new DateTime(2014, 6, 10) || post.DateTime.Date == new DateTime(2014, 6, 9));
                Assert.IsFalse(string.IsNullOrEmpty(post.Content));
                Assert.AreEqual(userName, post.Username);
                Assert.IsFalse(string.IsNullOrEmpty(post.Uri));
            }
            Assert.IsFalse(_errors.Any());
        }
    }
}
